local MPacketDefine = require("middle.MPacketDefine")
local MiddleLogic = require("middle.MiddleLogic")
local MiddleCopySign = require("middle.MiddleCopySign")
local MiddleFamilySign = require("middle.MiddleFamilySign")
local MiddleChat = require("middle.MiddleChat")
local MiddlePay = require("middle.MiddlePay")

function handle(fd)
	local ret, packetID = _G.msg_middle_parse.readint()
	if not ret then return end		
		
	local msg = {}
	if MPacketDefine.G2M_HELLO == packetID then
		local ret, svrIndex = _G.msg_middle_parse.readint()
		msg.svrIndex = svrIndex
		if not ret then return end
		MiddleLogic.G2M_HELLO(fd, msg)
	elseif MPacketDefine.G2M_DISCONNECT == packetID then
		MiddleLogic.G2M_DISCONNECT(fd)
	elseif MPacketDefine.G2M_HEARTBEAT == packetID then
		print("G2M heartbeat")
		local ret, openDay = _G.msg_middle_parse.readint()
		msg.openDay = openDay
		if not ret then return end
		local ret, avgZhandouli = _G.msg_middle_parse.readint()
		msg.avgZhandouli = avgZhandouli
		if not ret then return end
		MiddleLogic.G2M_HEARTBEAT(fd, msg)
	elseif MPacketDefine.G2M_LOGIN_MIDDLE == packetID then
		local ret, svrIndex = _G.msg_middle_parse.readint()
		msg.svrIndex = svrIndex
		if not ret then return end
		msg.db = {}
		ret = _G.msg_middle_parse.readtable(msg.db)
		if not ret then return end
		local ret, actID = _G.msg_middle_parse.readstring()
		msg.actID = actID
		if not ret then return end
		local ret, teamCopyID = _G.msg_middle_parse.readint()
		msg.teamCopyID = teamCopyID
		if not ret then return end
		MiddleLogic.G2M_LOGIN_MIDDLE(fd, msg)
	elseif MPacketDefine.M2G_LOGIN_MIDDLE_RET == packetID then	
		local ret, svrIndex = _G.msg_middle_parse.readint()
		msg.svrIndex = svrIndex
		if not ret then return end
		
		ret, retCode = _G.msg_middle_parse.readint()
		msg.retCode = retCode
		if not ret then return end
		
		ret, uuid = _G.msg_middle_parse.readstring()
		msg.uuid = uuid
		if not ret then return end		
		
		MiddleLogic.M2G_LOGIN_MIDDLE_RET(fd, msg)
	elseif MPacketDefine.G2M_LOGIN_MIDDLE_DATA == packetID then
		local ret, svrIndex = _G.msg_middle_parse.readint()
		msg.svrIndex = svrIndex
		if not ret then return end
		msg.db = {}
		ret = _G.msg_middle_parse.readtable(msg.db)
		if not ret then return end
		
		local ret, familyName = _G.msg_middle_parse.readstring()
		msg.familyName = familyName
		if not ret then return end	
		
		local ret, familyLv = _G.msg_middle_parse.readint()
		msg.familyLv = familyLv
		if not ret then return end
		
		MiddleLogic.G2M_LOGIN_MIDDLE_DATA(fd, msg)
	elseif MPacketDefine.M2G_LOGOUT_MIDDLE == packetID then
		local ret, svrIndex = _G.msg_middle_parse.readint()
		msg.svrIndex = svrIndex
		if not ret then return end
		msg.db = {}
		ret = _G.msg_middle_parse.readtable(msg.db)
		if not ret then return end
		MiddleLogic.M2G_LOGOUT_MIDDLE(fd, msg)	
	elseif MPacketDefine.G2M_LOGOUT_MIDDLE_RET == packetID then	
		local ret, svrIndex = _G.msg_middle_parse.readint()
		msg.svrIndex = svrIndex
		if not ret then return end
		
		ret, retCode = _G.msg_middle_parse.readint()
		msg.retCode = retCode
		if not ret then return end
		
		ret, uuid = _G.msg_middle_parse.readstring()
		msg.uuid = uuid
		if not ret then return end		
		
		MiddleLogic.G2M_LOGOUT_MIDDLE_RET(fd, msg)
	elseif MPacketDefine.M2G_LOGOUT_MIDDLE_DATA == packetID then	
		local ret, svrIndex = _G.msg_middle_parse.readint()
		msg.svrIndex = svrIndex
		if not ret then return end
		msg.db = {}
		ret = _G.msg_middle_parse.readtable(msg.db)
		if not ret then return end
		MiddleLogic.M2G_LOGOUT_MIDDLE_DATA(fd, msg)
	elseif MPacketDefine.G2M_LOGOUT_MIDDLE_DATA_RET == packetID then
		local ret, uuid = _G.msg_middle_parse.readstring()
		msg.uuid = uuid
		if not ret then return end
		MiddleLogic.G2M_LOGOUT_MIDDLE_DATA_RET(fd, msg)
	elseif MPacketDefine.M2G_LOGIN_MIDDLE_TIMEOUT == packetID then
		local ret, uuid = _G.msg_middle_parse.readstring()
		msg.uuid = uuid
		if not ret then return end
		MiddleLogic.M2G_LOGIN_MIDDLE_TIMEOUT(fd, msg)
	elseif MPacketDefine.G2M_LOGIN_MIDDLE_GM == packetID then
	  local ret, gmcmd = _G.msg_middle_parse.readstring()
	  if not ret then return end
	  msg.gmcmd = gmcmd
	  ret,gmcmd = _G.msg_middle_parse.readstring()
	  if not ret then return end
	  msg.arg = gmcmd
	  MiddleLogic.G2M_LOGIN_MIDDLE_GM(fd, msg)
	elseif MPacketDefine.M2G_SEND_MAIL == packetID then
	  local ret, uuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    msg.uuid = uuid
    ret,msg.title = _G.msg_middle_parse.readstring()
    if not ret then return end
    
    ret,msg.content = _G.msg_middle_parse.readstring()
    if not ret then return end
    
    msg.gridlist = {}
    ret = _G.msg_middle_parse.readtable(msg.gridlist)
    if not ret then return end
    
    if #msg.gridlist == 0 then msg.gridlist = nil end
    
    MiddleLogic.sendMail(msg.uuid, msg.title, msg.content, msg.gridlist)
  elseif MPacketDefine.G2M_COPY_SIGN == packetID then
    local ret, copyID = _G.msg_middle_parse.readint()
    if not ret then return end
    msg.copyID = copyID
    msg.list = {}
    ret = _G.msg_middle_parse.readtable(msg.list)
    if not ret then return end
    MiddleCopySign.G2M_COPY_SIGN(fd, msg)
  elseif MPacketDefine.M2G_COPY_SIGN_LIST == packetID then
    local ret, copyID = _G.msg_middle_parse.readint()
    if not ret then return end
    msg.copyID = copyID
    msg.list = {}
    ret = _G.msg_middle_parse.readtable(msg.list)
    if not ret then return end
    MiddleCopySign.M2G_COPY_SIGN_LIST(fd, msg)
  elseif MPacketDefine.G2M_COPY_SIGN_EXIT == packetID then
    local ret, copyID = _G.msg_middle_parse.readint()
    if not ret then return end
    msg.copyID = copyID
    ret, msg.uuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    MiddleCopySign.G2M_COPY_SIGN_EXIT(fd, msg)
  elseif MPacketDefine.M2G_COPY_SIGN_EXIT_RET == packetID then
    local ret, copyID = _G.msg_middle_parse.readint()
    if not ret then return end
    msg.copyID = copyID
    ret, msg.uuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret,msg.exitType = _G.msg_middle_parse.readint()
    if not ret then return end
    MiddleCopySign.M2G_COPY_SIGN_EXIT_RET(fd, msg)
  elseif MPacketDefine.G2M_COPY_SIGN_READY == packetID then
    local ret, copyID = _G.msg_middle_parse.readint()
    if not ret then return end
    msg.copyID = copyID
    ret, msg.uuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    MiddleCopySign.G2M_COPY_SIGN_READY(fd, msg)
  elseif MPacketDefine.M2G_COPY_SIGN_READY == packetID then
    local ret, copyID = _G.msg_middle_parse.readint()
    if not ret then return end
    msg.copyID = copyID
    ret, msg.uuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.leaderUuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    MiddleCopySign.M2G_COPY_SIGN_READY(fd, msg)
  elseif MPacketDefine.M2G_COPY_SIGN_ERR == packetID then
    local ret
    ret, msg.uuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.type = _G.msg_middle_parse.readint()
    if not ret then return end
    ret, msg.err = _G.msg_middle_parse.readstring()
    if not ret then return end
    MiddleCopySign.M2G_COPY_SIGN_ERR(fd, msg)
  elseif MPacketDefine.M2G_COPY_SIGN_ROOM_OK == packetID then
    local ret
    ret, msg.uuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.copyID = _G.msg_middle_parse.readint()
    if not ret then return end
    ret, msg.roomID = _G.msg_middle_parse.readint()
    if not ret then return end
    MiddleCopySign.M2G_COPY_SIGN_ROOM_OK(fd, msg)
    
    
  elseif MPacketDefine.G2M_MIDDLE_CHAT == packetID then
    local ret
    ret, msg.svrIndex = _G.msg_middle_parse.readint()
    if not ret then return end
    ret, msg.type = _G.msg_middle_parse.readint()
    if not ret then return end
    ret, msg.roleName = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.blue = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.content = _G.msg_middle_parse.readstring()
    if not ret then return end
    MiddleChat.G2M_MIDDLE_CHAT(fd, msg)
  elseif MPacketDefine.M2G_MIDDLE_CHAT == packetID then
    local ret
    ret, msg.svrIndex = _G.msg_middle_parse.readint()
    if not ret then return end
    ret, msg.type = _G.msg_middle_parse.readint()
    if not ret then return end
    ret, msg.roleName = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.blue = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.content = _G.msg_middle_parse.readstring()
    if not ret then return end
    MiddleChat.M2G_MIDDLE_CHAT(fd, msg)
  elseif MPacketDefine.G2M_FAMILY_WAR_SIGN == packetID then
    local ret
    ret, msg.svrIndex = _G.msg_middle_parse.readint() 
    if not ret then return end
    ret, msg.familyUuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.familyName = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.uuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    MiddleFamilySign.G2M_FAMILY_WAR_SIGN(fd, msg)
  elseif MPacketDefine.M2G_FAMILY_WAR_SIGN == packetID then
    local ret
    ret, msg.familyUuid = _G.msg_middle_parse.readstring()
    if not ret then return end   
    ret, msg.uuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    MiddleFamilySign.M2G_FAMILY_WAR_SIGN(fd, msg)
  elseif MPacketDefine.G2M_FAMILY_WAR_LIST == packetID then
    MiddleFamilySign.G2M_FAMILY_WAR_LIST(fd, msg)
  elseif MPacketDefine.M2G_FAMILY_WAR_LIST == packetID then
    local ret
    ret,msg.status = _G.msg_middle_parse.readint()
    if not ret then return end
    msg.signData = {}
    ret = _G.msg_middle_parse.readtable(msg.signData)
    if not ret then return end   
    msg.warData = {}
    ret= _G.msg_middle_parse.readtable(msg.warData)
    if not ret then return end
    MiddleFamilySign.M2G_FAMILY_WAR_LIST(fd, msg)
  elseif MPacketDefine.M2G_FAMILY_WAR_NOTIFY == packetID then
    local ret
    ret, msg.type = _G.msg_middle_parse.readint()
    if not ret then return end 
    ret, msg.round = _G.msg_middle_parse.readint()
    if not ret then return end   
    ret, msg.familyUuid1 = _G.msg_middle_parse.readstring()
    if not ret then return end 
    ret, msg.familyUuid2 = _G.msg_middle_parse.readstring()
    if not ret then return end 
    MiddleFamilySign.M2G_FAMILY_WAR_NOTIFY(fd, msg)
  elseif MPacketDefine.G2M_FAMILY_DATA == packetID then
    local ret
    ret, msg.familyUuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.zhandouli = _G.msg_middle_parse.readint()
    if not ret then return end
    ret, msg.leaderUuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.leaderName = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.leaderFuUuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.leaderFuName = _G.msg_middle_parse.readstring()
    if not ret then return end
    MiddleFamilySign.G2M_FAMILY_DATA(fd, msg)
  elseif MPacketDefine.M2G_PAY == packetID then
    local ret
    ret, msg.uuid = _G.msg_middle_parse.readstring()
    if not ret then return end
    ret, msg.rmb = _G.msg_middle_parse.readint()
    if not ret then return end
    ret, msg.yb = _G.msg_middle_parse.readint()
    if not ret then return end
    MiddlePay.M2G_PAY(msg.uuid, msg.rmb, msg.yb)
  end
end